package apigateway.project.commen.utils;

public class pathPattern {
    /**
     * 匹配算法，匹配两个路由是否一样，可以使用通配符/**以及/*
     * @param path 请求的路径
     * @param pattern 断言中的path
     * @return
     */
    public static boolean pattern(String path,String pattern) {
        String[] s1 = path.split("/");
        String[] s2 = pattern.split("/");
        String[]s3 = null;
        String[]s4 = null;
        if(firstJudge(path)&&firstJudge(pattern)) {
            return judge(s1,s2);
        }
        else if(!firstJudge(path)&&firstJudge(pattern)) {
            s3 = new String[s1.length-1];
            System.arraycopy(s1,1,s3,0,s3.length);
            return judge(s3,s2);
        }
        else if(firstJudge(path)&&!firstJudge(pattern)) {
            s4 = new String[s2.length-1];
            System.arraycopy(s2,1,s4,0,s4.length);
            return judge(s1,s4);
        }
        else{
            s3 = new String[s1.length-1];
            s4 = new String[s2.length-1];
            System.arraycopy(s1,1,s3,0,s3.length);
            System.arraycopy(s2,1,s4,0,s4.length);
            return judge(s3,s4);
        }
    }


    /**
     * 判断传入的字符第一个字符是不是以'/'开头的，如果是则返回false，反之则返回true
     * @param s
     * @return
     */
    public static boolean firstJudge(String s) {
        return s.charAt(0) != '/';
    }

    public static boolean judge(String[]path,String[]pattern) {
        int pathpos = 0;
        int patternpos = 0;
        while(pathpos < path.length) {
            if ("*".equals(pattern[patternpos])) {
                if (patternpos == pattern.length - 1 && pathpos == path.length - 1) {
                    return true;
                } else if (pathpos == path.length - 1 && patternpos != pattern.length - 1) {
                    for (int j = patternpos + 1; j < pattern.length; j++) {
                        if (!"**".equals(pattern[j])) {
                            return false;
                        }
                    }
                    return true;
                }
                else if(patternpos == pattern.length - 1 && pathpos != path.length - 1) {
                    return false;
                }
                else{
                    pathpos++;
                    patternpos++;
                }
            } else if ("**".equals(pattern[patternpos])) {
                if(pathpos == path.length - 1 && patternpos == pattern.length - 1) {
                    return true;
                }
                else if(pathpos == path.length - 1 && patternpos != pattern.length - 1) {
                    for (int j = patternpos + 1; j < pattern.length; j++) {
                        if (!"**".equals(pattern[j])) {
                            return false;
                        }
                    }
                    return true;
                }
                else if(patternpos == pattern.length - 1 && pathpos != path.length - 1) {
                    return true;
                }
                else{
                    while(patternpos<pattern.length&&pattern[patternpos].equals("**")){
                        patternpos++;
                    }
                    while(pathpos<path.length&&!path[pathpos].equals(pattern[patternpos])){
                        pathpos++;
                    }
                }
            } else {
                if (path[pathpos].equals(pattern[patternpos])) {
                    if(pathpos == path.length - 1 && patternpos == pattern.length - 1) {
                        return true;
                    }
                    else if(pathpos != path.length - 1 && patternpos == pattern.length - 1) {
                        return false;
                    }
                    else if(pathpos == path.length - 1 && patternpos != pattern.length - 1) {
                        for (int j = patternpos + 1; j < pattern.length; j++) {
                            if (!"**".equals(pattern[j])) {
                                return false;
                            }
                        }
                        return true;
                    }else{
                        pathpos++;
                        patternpos++;
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

}
